﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using HCFramework.API.LINQ;

namespace C
{
    public class TrangController
    {
        public static void Insert(Trang a)
        {
            if (GetByURL(a.URL) != null) return; // kiểm tra tồn tại

            HCDataContext db = new HCDataContext();
            db.Trangs.InsertOnSubmit(a);
            db.SubmitChanges();

            Role role = RoleController.GetByName(RoleController.GetAdminRoleName(), db);
            if (role != null)
            {
                TrangRole trangRole = new TrangRole();
                trangRole.TrangID = a.TrangID;
                trangRole.RoleID = role.RoleID;
                TrangRoleController.Insert(trangRole);
            }
        }

        public static void Update(Trang a, HCDataContext db)
        {
            db.SubmitChanges();
        }

        public static void Delete(int id)
        {
            HCDataContext db = new HCDataContext();
            Trang a = db.Trangs.Where(
                ik => ik.TrangID == id).First();
            db.TrangRoles.DeleteAllOnSubmit(a.TrangRoles);
            db.Trangs.DeleteOnSubmit(a);
            db.SubmitChanges();
        }

        public static Trang GetByID(int id, HCDataContext db)
        {
            return db.Trangs.Where(
                ik => ik.TrangID == id).FirstOrDefault();
        }

        public static Trang GetByID(int id)
        {
            return GetByID(id, new HCDataContext());
        }

        public static Trang GetByURL(string url)
        {
            return new HCDataContext().Trangs.Where(
                ik => ik.URL == url).FirstOrDefault();
        }

        internal static string GetRoleName(int trangID)
        {
            string ret = "";
            Trang trang = GetByID(trangID);
            foreach (TrangRole trangRole in trang.TrangRoles.OrderBy(ik => ik.Role.Name))
            {
                ret += ", " + trangRole.Role.Name;
            }
            return ret.TrimStart(',');
        }

        internal static List<string> GetListRoleName(string trangName)
        {
            List<string> ret = new List<string>();
            ret.Add(RoleController.GetAdminRoleName());
            HCDataContext db = new HCDataContext();
            Trang trang = db.Trangs.Where(ik => ik.URL == trangName).FirstOrDefault();
            if (trang == null) return ret;
            foreach (TrangRole trangRole in trang.TrangRoles)
            {
                ret.Add(trangRole.Role.Name);
            }
            return ret;
        }

        public static string GetCurrentPage()
        {
            return "~" + HttpContext.Current.Request.Url.AbsolutePath;
        }

        public static Trang GetCurrentTrangOrInsert()
        {
            HCDataContext dc = new HCDataContext();
            Trang trang = dc.Trangs.Where(i => i.URL == GetCurrentPage()).FirstOrDefault();
            if (trang == null)
            {
                trang = new Trang();
                trang.URL = GetCurrentPage();
                Insert(trang);
            }
            return trang;
        }

        public static Trang GetCurrentTrangOrInsert(HCDataContext dc)
        {
            Trang trang = dc.Trangs.Where(i => i.URL == GetCurrentPage()).FirstOrDefault();
            if (trang == null)
            {
                trang = new Trang();
                trang.URL = GetCurrentPage();
                Insert(trang);
            }
            return trang;
        }

        public static bool IsHomePage()
        {
            string currPath = HttpContext.Current.Request.Url.AbsolutePath;
            return currPath.ToLower() == "/default.aspx";
        }
    }
}